package cn.jy.nvr.service.impl;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.jy.nvr.datasource.DataSourceConst;
import cn.jy.nvr.datasource.DataSourceContextHolder;
import cn.jy.nvr.domain.APHERESESTask;
import cn.jy.nvr.domain.Channel;
import cn.jy.nvr.domain.DownloadItem;
import cn.jy.nvr.downfile.DownFile;
import cn.jy.nvr.mapper.APHERESESTaskMapper;
import cn.jy.nvr.mapper.ChannelMapper;
import cn.jy.nvr.service.DownloadVideoQuartzJob;
import cn.jy.nvr.utils.DownloadUtils;
@Service("DownloadVideoQuartzJob")
public class DownloadVideoQuartzJobImpl implements DownloadVideoQuartzJob {
	static{DownFile.getInstance().initAndLogin();}
	
	@Autowired
	APHERESESTaskMapper apheresesTaskMapper;
	@Autowired
	ChannelMapper channelMapper;
	Logger logger=Logger.getLogger(DownloadVideoQuartzJobImpl.class);
	@Override
	public void work() {
		 logger.info("---开始下载任务---");
		 DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
		 List<APHERESESTask> downloadTaskList = apheresesTaskMapper.loadAPHERESESDownloadTask();
		 
		 
		 if (!downloadTaskList.isEmpty()) {
			for (APHERESESTask apheresesTask : downloadTaskList) {
				logger.info("开始下载卡号:"+apheresesTask.getCardId()+"视频");
				//根据机器号拿到设备对应的通道号
				DataSourceContextHolder.setDataSourceType(DataSourceConst.mx);
				Channel channeliInfo = channelMapper.getChannelByMACHINELOGID(apheresesTask.getMachineNo());
				
				//生成下载文件的目录
				if (channeliInfo==null) {
					continue;
				}
				 int year = 0;
				 int month = 0;
				 int day = 0;
				 try {
					Date date= new SimpleDateFormat("yyyy-MM-dd").parse(apheresesTask.getCollectionStartTime().substring(0, 10));
					Calendar filetime=Calendar.getInstance();
					filetime.setTime(date);
					 year=filetime.get(Calendar.YEAR);
					 month=(filetime.get(Calendar.MONTH) + 1);
					 day=filetime.get(Calendar.DAY_OF_MONTH);
				} catch (ParseException e) {
					e.printStackTrace();
				}
				 String calendarPath=year+"/"+month+"/"+day+"/";
				 File file=new File("D:/apache-tomcat-7.0.79/webapps/Nvr/video/"+calendarPath);
				 if(!file.exists()){
					 file.mkdirs();
				 }
				//生成文件前面的文件名
				 String fileName=year+""+month+""+day+""+apheresesTask.getCardId();
				 
				 //安装耗材时间下载视频
				 String  installStartTime=apheresesTask.getInstallStartTime();
				 String  installEndTime=apheresesTask.getInstallEndTime();
					 //判断两个时间是都未空
					 if ( !DownloadUtils.timeIsEmpty(installStartTime, installEndTime)) {
						   DownloadItem downloadItem = new DownloadItem();
						     int channel=Integer.valueOf(channeliInfo.getChannel1());
							 downloadItem.setChannel(channel);
							 downloadItem.setStart(installStartTime);
							 downloadItem.setEnd(installEndTime);
							 downloadItem.setFilename(fileName+"i");
							 downloadItem.setFilepath(file.getAbsolutePath()+"\\");
							 DownFile.getInstance().downFileByTime(downloadItem);
							 logger.info("---安装耗材视频---"+apheresesTask.getCardId());
					}
					    try {
							Thread.sleep(1000);
						} catch (InterruptedException e1) {
							e1.printStackTrace();
						}
				 //安装耗材后空闲时间
		        String installSpaceStartTime = apheresesTask.getInstallSpaceStartTime();	
		        String installSpaceEndTime = apheresesTask.getInstallSpaceEndTime();
				     if (!DownloadUtils.timeIsEmpty(installSpaceStartTime, installSpaceEndTime)) {
				    	//浆机视频
				    	 DownloadItem downloadItem = new DownloadItem();
					     int channel=Integer.valueOf(channeliInfo.getChannel1());
						 downloadItem.setChannel(channel);
						 downloadItem.setStart(installSpaceStartTime);
						 downloadItem.setEnd(installSpaceEndTime);
						 downloadItem.setFilename(fileName+"is");
						 downloadItem.setFilepath(file.getAbsolutePath()+"\\");
						 DownFile.getInstance().downFileByTime(downloadItem);
						 logger.info("---安装耗材空闲视频---"+apheresesTask.getCardId());
						 //环境视频
						 DownloadItem downloadItem1 = new DownloadItem();
					     int channel1=Integer.valueOf(channeliInfo.getChannel2());
						 downloadItem1.setChannel(channel1);
						 downloadItem1.setStart(installSpaceStartTime);
						 downloadItem1.setEnd(installSpaceEndTime);
						 downloadItem1.setFilename(fileName+"ise");
						 downloadItem1.setFilepath(file.getAbsolutePath()+"\\");
						 DownFile.getInstance().downFileByTime(downloadItem1);
						 logger.info("---安装耗材视频环境---"+apheresesTask.getCardId());
					}
				     try {
							Thread.sleep(1000);
						} catch (InterruptedException e1) {
							e1.printStackTrace();
					}
				     
				     //穿刺视频
				     String puctureStartTime=apheresesTask.getPunctureStartTime();
					 String  puctureEndTime=apheresesTask.getPunctureEndTime();
					 if (!DownloadUtils.timeIsEmpty(puctureStartTime, puctureEndTime)) {
						 DownloadItem downloadItem = new DownloadItem();
					     int channel=Integer.valueOf(channeliInfo.getChannel1());
						 downloadItem.setChannel(channel);
						 downloadItem.setStart(puctureStartTime);
						 downloadItem.setEnd(puctureEndTime);
						 downloadItem.setFilename(fileName+"p");
						 downloadItem.setFilepath(file.getAbsolutePath()+"\\");
						 DownFile.getInstance().downFileByTime(downloadItem);
						 logger.info("---穿刺视频---"+apheresesTask.getCardId());
					 }
					 try {
							Thread.sleep(1000);
						} catch (InterruptedException e1) {
							e1.printStackTrace();
					}
					 //采浆视频
					 String collectionStartTime = apheresesTask.getCollectionStartTime();
					 String collectionEndTime = apheresesTask.getCollectionEndTime();
					 if (!DownloadUtils.timeIsEmpty(collectionStartTime, collectionEndTime)) {
						 DownloadItem downloadItem = new DownloadItem();
					     int channel=Integer.valueOf(channeliInfo.getChannel1());
						 downloadItem.setChannel(channel);
						 downloadItem.setStart(collectionStartTime);
						 downloadItem.setEnd(collectionEndTime);
						 downloadItem.setFilename(fileName+"c");
						 downloadItem.setFilepath(file.getAbsolutePath()+"\\");
						 DownFile.getInstance().downFileByTime(downloadItem);
						 logger.info("---采浆视频---"+apheresesTask.getCardId());
						 DownloadItem downloadItem1 = new DownloadItem();
					     int channel1=Integer.valueOf(channeliInfo.getChannel2());
						 downloadItem1.setChannel(channel1);
						 downloadItem1.setStart(collectionStartTime);
						 downloadItem1.setEnd(collectionEndTime);
						 downloadItem1.setFilename(fileName+"ce");
						 downloadItem1.setFilepath(file.getAbsolutePath()+"\\");
						 DownFile.getInstance().downFileByTime(downloadItem1);
						 logger.info("---采浆坏境视频---"+apheresesTask.getCardId());
					}
					 try {
							Thread.sleep(1000);
						} catch (InterruptedException e1) {
							e1.printStackTrace();
					}
					 //卸载耗材视频
					 String uninstallStartTime = apheresesTask.getUninstallStartTime();
					 String uninstallEndTime = apheresesTask.getUninstallEndTime();
					 if (!DownloadUtils.timeIsEmpty(uninstallStartTime, uninstallEndTime)) {
						 DownloadItem downloadItem = new DownloadItem();
					     int channel=Integer.valueOf(channeliInfo.getChannel1());
						 downloadItem.setChannel(channel);
						 downloadItem.setStart(uninstallStartTime);
						 downloadItem.setEnd(uninstallEndTime);
						 downloadItem.setFilename(fileName+"u");
						 downloadItem.setFilepath(file.getAbsolutePath()+"\\");
						 DownFile.getInstance().downFileByTime(downloadItem);
						 logger.info("---卸载耗材视频---"+apheresesTask.getCardId());
					}
					 try {
							Thread.sleep(1000);
						} catch (InterruptedException e1) {
							e1.printStackTrace();
					}
					//如果有空闲的就下空闲视频 
					String spaceStartTime = apheresesTask.getSpaceStartTime();
					String spaceEndTime = apheresesTask.getSpaceEndTime();
					if (!DownloadUtils.timeIsEmpty(spaceStartTime, spaceEndTime)) {
						DownloadItem downloadItem = new DownloadItem();
					     int channel=Integer.valueOf(channeliInfo.getChannel1());
						 downloadItem.setChannel(channel);
						 downloadItem.setStart(spaceStartTime);
						 downloadItem.setEnd(spaceEndTime);
						 downloadItem.setFilename(apheresesTask.getMachineNo()+apheresesTask.getSpaceSequence()+"s");
						 downloadItem.setFilepath(file.getAbsolutePath()+"\\");
						 DownFile.getInstance().downFileByTime(downloadItem);
						 logger.info("---空闲视频---"+apheresesTask.getCardId());
						 DownloadItem downloadItem1 = new DownloadItem();
					     int channel1=Integer.valueOf(channeliInfo.getChannel2());
						 downloadItem1.setChannel(channel1);
						 downloadItem1.setStart(spaceStartTime);
						 downloadItem1.setEnd(spaceEndTime);
						 downloadItem1.setFilename(apheresesTask.getMachineNo()+apheresesTask.getSpaceSequence()+"se");
						 downloadItem1.setFilepath(file.getAbsolutePath()+"\\");
						 DownFile.getInstance().downFileByTime(downloadItem1);
						 logger.info("---空闲环境视频---"+apheresesTask.getCardId());
					}
					
					 logger.info("---完成视频下载---"+apheresesTask.getCardId());
					 apheresesTaskMapper.updateFlag(apheresesTask);
			}
			 
		}
		 
		
	}

}
